from django.shortcuts import render

# Create your views here.
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView, GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from news import serializers
from news.models import Channels, News, Comments
from news.serializers import ChannelListSerializer, NewsListSerializer, NewsDetailSerializer


# , CommentsListSerializer, CommentsSerializer


# url(r'^channels/$')
class ChannelListView(ListAPIView):
    """分类列表"""
    serializer_class = ChannelListSerializer
    queryset = Channels.objects.all()


# url(r'^channels/(?P<channel>\d+)/$')
class NewsListView(ListAPIView):
    serializer_class = NewsListSerializer

    def get_queryset(self):
        channel_id = self.kwargs['channel_id']
        news_list = News.objects.filter(channel_id=channel_id).order_by('-create_time')
        return news_list


# url(r'^news/(?P<pk>\d+)/$'),
class NewsDetailView(RetrieveAPIView):
    queryset = News.objects.all()
    serializer_class = NewsDetailSerializer


# # url(r'^comments/$')
# class CommentsListView(ListAPIView):
#     serializer_class = CommentsListSerializer
#
#     def get_queryset(self):
#         news_id = self.kwargs['news_id']
#         comment_list = Comments.objects.filter(news_id=news_id)
#         return comment_list


class CreateCommentAPIView(GenericAPIView):
    # permission_classes = [IsAuthenticated]
    serializer_class = serializers.CommentSubmitSerializer

    def post(self, request):

        #  data
        #  serializer.validated_data
        #  serializer.data

        serializer = serializers.CommentSubmitSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data)



















